Python में Inheritance क्या है? | Code Reuse & Parent-Child Class Explained
Python Inheritance
OOP के चार मुख्य स्तंभों में से एक और बहुत important concept है — Inheritance। यह वही concept है जिसने पिछले lesson के Method Overriding वाले example में Dog(Animal) को possible बनाया था। अब इसे पूरी तरह, step-by-step समझते हैं।
अगर आपने अभी Python Classes & Objects नहीं पढ़ा है, तो पहले उसे पढ़ना जरूरी है, क्योंकि Inheritance पूरी तरह Classes पर आधारित है। Examples खुद run करने के लिए हमारा Online Python Editor & Compiler use कर सकते हैं।
super(), और Multilevel Inheritance के basic idea को ही कवर किया जाएगा। Encapsulation, Abstraction और Multiple/Hierarchical Inheritance जैसी advanced details को आगे dedicated lessons में अलग से समझाया जाएगा।
Inheritance क्या है?
Inheritance OOP का वह सिद्धांत है, जिसमें एक Class दूसरी Class की properties और behavior (attributes और methods) को अपने-आप प्राप्त (inherit) कर लेती है, बिना उन्हें दोबारा लिखे।
सरल भाषा में — Inheritance का मतलब है: "मुझे अपने माता-पिता से कुछ चीज़ें विरासत में मिल जाती हैं, बिना मुझे उन्हें दोबारा बनाना पड़े।"
Parent Class क्या है?
Parent Class (जिसे Base Class या Super Class भी कहते हैं) वह Class है, जिसकी properties और methods किसी दूसरी Class को दी जाती हैं।
यहाँ Animal एक Parent Class है।
Child Class क्या है?
Child Class (जिसे Derived Class या Sub Class भी कहते हैं) वह Class है, जो किसी Parent Class से properties और methods को inherit करती है।
Output:
class Dog(Animal): में Animal को brackets में लिखना ही Dog को Animal का Child Class बना देता है। Dog में खुद कोई eat() method नहीं लिखा, फिर भी वह इस्तेमाल हो गया — यही Inheritance है।
Parent से Methods और Attributes Inherit करना
Child Class को Parent Class के methods ही नहीं, attributes भी अपने-आप मिल जाते हैं।
Output:
Dog ने set_name() और show_name() — दोनों methods Animal से inherit कर लिए, बिना उन्हें दोबारा लिखे।
Child Class में अपना Feature जोड़ना
Child Class सिर्फ Parent की चीज़ें इस्तेमाल ही नहीं करती — वह अपने खुद के नए methods और attributes भी जोड़ सकती है।
Output:
eat() method Animal से inherit हुआ, जबकि bark() सिर्फ Dog के पास है — Animal के पास bark() नहीं है।
Code Reuse
Inheritance का सबसे बड़ा practical फायदा है Code Reuse — यानी एक ही code को बार-बार लिखने की बजाय, उसे एक जगह लिखकर कई Classes में reuse करना।
यहाँ eat() method दोनों Classes में अलग-अलग लिखा गया — same code दो बार।
Output:
eat() सिर्फ एक बार, Animal में लिखा गया, और Dog व Cat दोनों ने उसे reuse किया — यही Inheritance का मुख्य फायदा है।
Method Overriding का Basic Concept
कभी-कभी Child Class को Parent के किसी method का behavior बदलना होता है — इसके लिए Child Class में उसी नाम का method फिर से define कर दिया जाता है, जिसे Method Overriding कहते हैं।
Output:
Dog ने Animal के sound() method को अपने तरीके से फिर से लिख दिया — Python हमेशा Child Class के method को पहले priority देता है।
super() का Basic Use
super() का इस्तेमाल Child Class के अंदर से Parent Class के method को call करने के लिए किया जाता है — भले ही Child Class ने उस method को override कर दिया हो।
Output:
super().sound() ने पहले Parent Class Animal के original sound() को call किया, फिर Child Class का अपना code चला। इस तरह Dog अपने Parent की functionality को पूरी तरह हटाने की बजाय, उसे extend कर पाया।
Multilevel Inheritance का Basic Idea
Multilevel Inheritance में एक Class किसी दूसरी Child Class से आगे inherit करती है — यानी Inheritance की एक chain बन जाती है (Parent → Child → Grandchild)।
Output:
Puppy को Dog से bark() मिला, और Dog के through Animal से eat() भी मिल गया — इसी chain को Multilevel Inheritance कहते हैं।
Real-World Employee Example
अब Inheritance को एक practical, real-world जैसी situation में देखते हैं — एक company में अलग-अलग तरह के employees।
Output:
Manager एक special तरह का Employee है — उसे Employee की सारी basic details (set_details(), show_details()) मिल गईं, और साथ में उसने अपना खुद का feature (team_size) भी जोड़ लिया। यही Inheritance का असली, practical इस्तेमाल है।
Common Beginner Mistakes
Child Class में नया method जोड़ने या किसी method को override करने से Parent Class पर कोई असर नहीं पड़ता। Inheritance सिर्फ एक दिशा में चलता है — Parent से Child की तरफ।
Child के नए methods सिर्फ Child के Objects पर ही काम करते हैं — Parent के Objects को वे features नहीं मिलते।
Exercise ?
Child Class से Parent Class के किसी method को call करने के लिए किसका इस्तेमाल किया जाता है?
Practical Coding Challenge
अब खुद अपने Online Python Editor में यह challenge solve कीजिए:
- एक
VehicleParent Class बनाइए जिसमेंset_details(self, brand, speed)औरshow_details(self)methods हों। - एक
Carनाम की Child Class बनाइए जोVehicleसे inherit करे, और उसमें अपना एक नया methodhonk(self)जोड़िए। Carका एक Object बनाकर Parent और Child — दोनों के methods को call कीजिए।Vehicleमेंsound()नाम का एक method बनाइए, औरCarमें उसे override करकेsuper()की मदद से Parent का version भी साथ में call कीजिए।- एक
SportsCarनाम की तीसरी Class बनाइए जोCarसे inherit करे (Multilevel Inheritance), और उसमें एक नया feature जोड़िए।
Quick Revision
| Concept | मतलब |
|---|---|
| Inheritance | एक Class द्वारा दूसरी Class की properties/methods प्राप्त करना |
| Parent Class | वह Class जिससे features दिए जाते हैं |
| Child Class | वह Class जो features प्राप्त करती है — class Child(Parent): |
| Method Overriding | Child द्वारा Parent के method को फिर से define करना |
| super() | Child से Parent के method को call करने के लिए |
| Multilevel Inheritance | Parent → Child → Grandchild जैसी chain |
FAQ
Python में Inheritance क्या है?
Inheritance OOP का वह सिद्धांत है, जिसमें एक Class (Child) दूसरी Class (Parent) की properties और methods को अपने-आप प्राप्त कर लेती है।
Parent Class और Child Class में क्या फर्क है?
Parent Class वह है जिसकी properties और methods share की जाती हैं, जबकि Child Class वह है जो उन्हें inherit करती है और अपने खुद के नए features भी जोड़ सकती है।
Inheritance का सबसे बड़ा फायदा क्या है?
Inheritance का सबसे बड़ा फायदा Code Reuse है — एक जैसा code बार-बार अलग-अलग Classes में लिखने की बजाय, उसे एक जगह लिखकर कई Classes में reuse किया जा सकता है।
super() का इस्तेमाल क्यों किया जाता है?
super() का इस्तेमाल Child Class से Parent Class के method को call करने के लिए किया जाता है, खासकर तब जब वह method override भी किया गया हो।
Multilevel Inheritance क्या है?
Multilevel Inheritance में एक Class किसी ऐसी Class से inherit करती है, जो खुद भी किसी और Class से inherit कर रही होती है — जिससे Parent → Child → Grandchild जैसी एक chain बन जाती है।
Summary
Inheritance = Child Class को Parent Class से features विरासत में मिलना, बिना उन्हें दोबारा लिखे।
इस lesson में आपने सीखा कि Inheritance क्या है, Parent और Child Class क्या होते हैं, Methods और Attributes कैसे inherit होते हैं, Child Class में अपना feature कैसे जोड़ें, Code Reuse कैसे होता है, Method Overriding और super() कैसे काम करते हैं, और Multilevel Inheritance का basic idea क्या है।
Inheritance OOP का एक बहुत powerful tool है जो code को organized और reusable बनाता है। अब ऊपर दिया गया coding challenge खुद अपने Online Editor में solve करें।
Related Articles
Python code likho, run karo aur practice karo — directly browser mein.

कोई टिप्पणी नहीं:
एक टिप्पणी भेजें